package com.zhou.jiancai.activity;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.SectionDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.Nameable;
import com.zhou.jiancai.R;
import com.zhou.jiancai.base.BaseActivity;
import com.zhou.jiancai.fragment.FriendFragment;
import com.zhou.jiancai.fragment.HomeFragment;
import com.zhou.jiancai.fragment.MeFragment;
import com.zhou.jiancai.fragment.PariseFragment;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


public class MainActivity extends BaseActivity implements View.OnClickListener {


    private static final long PROFILE_SETTING = 1;
    @InjectView(R.id.container)
    FrameLayout container;
    @InjectView(R.id.iv_1)
    ImageView iv1;
    @InjectView(R.id.tv_home)
    TextView tvHome;
    @InjectView(R.id.ll_home)
    LinearLayout llHome;
    @InjectView(R.id.iv_2)
    ImageView iv2;
    @InjectView(R.id.tv_praise)
    TextView tvPraise;
    @InjectView(R.id.ll_praise)
    LinearLayout llPraise;
    @InjectView(R.id.iv_3)
    ImageView iv3;
    @InjectView(R.id.ll_friend)
    LinearLayout llFriend;
    @InjectView(R.id.iv_4)
    ImageView iv4;
    @InjectView(R.id.ll_me)
    LinearLayout llMe;
    @InjectView(R.id.activity_main)
    RelativeLayout activityMain;
    private RadioButton radioButton_person;
    private RadioButton radioButton_products;
    private FrameLayout frameLayout_container;
    private MeFragment personFragment;
    private HomeFragment homeFragment;
    private PariseFragment pariseFragment;
    private FriendFragment friendFragment;
    private Drawer result;
    private AccountHeader headerResult;


public final static String TagHome="taghome";
public final static String Tagfriend="tagfriend";
public final static String Tagpraise="tagparise";
public final static String Tagperson="tagme";
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




        //setContentView(R.layout.activity_main);
        //设置沉浸式
        //  StatusBarUtil.setColor(SlideViewActivity.this,getResources().getColor(R.color.colorbar));
        //设置半透明    0-255   0表示完全不透明 255表示完全透明
        //  StatusBarUtil.setTranslucent(SlideViewActivity.this, 100);
        //设置全透明
        // StatusBarUtil.setTransparent(SlideViewActivity.this);
        //隐藏伪状态栏
        // StatusBarUtil.hideFakeStatusBarView(SlideViewActivity.this);

       // StatusBarUtil.setColor(SlideViewActivity.this, getResources().getColor(R.color.transparent));




        //侧拉栏
        new DrawerBuilder().withActivity(this).build();



      /*  final IProfile profile = new ProfileDrawerItem().withName("Mike Penz").withEmail("mikepenz@gmail.com").withIcon("https://avatars3.githubusercontent.com/u/1476232?v=3&s=460");
        final IProfile profile2 = new ProfileDrawerItem().withName("Bernat Borras").withEmail("alorma@github.com").withIcon(Uri.parse("https://avatars3.githubusercontent.com/u/887462?v=3&s=460"));
        final IProfile profile3 = new ProfileDrawerItem().withName("Max Muster").withEmail("max.mustermann@gmail.com");
        final IProfile profile4 = new ProfileDrawerItem().withName("Felix House").withEmail("felix.house@gmail.com");
        final IProfile profile5 = new ProfileDrawerItem().withName("Mr. X").withEmail("mister.x.super@gmail.com");
        final IProfile profile6 = new ProfileDrawerItem().withName("Batman").withEmail("batman@gmail.com");

        headerResult = new AccountHeaderBuilder()
                .withActivity(this)
                .withHeaderBackground(R.mipmap.abc)
                .withTranslucentStatusBar(false)
                .addProfiles(
                        profile,
                        profile2,
                        profile3,
                        profile4,
                        profile5,
                        profile6,
                        //don't ask but google uses 14dp for the add account icon in gmail but 20dp for the normal icons (like manage account)
                        new ProfileSettingDrawerItem().withName("Add Account").withDescription("Add new GitHub Account"),
                        new ProfileSettingDrawerItem().withName("Manage Account")
                )
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean current) {
                        //sample usage of the onProfileChanged listener
                        //if the clicked item has the identifier 1 add a new profile ;)
                        if (profile instanceof IDrawerItem && ((IDrawerItem) profile).getIdentifier() == PROFILE_SETTING) {
                            IProfile newProfile = new ProfileDrawerItem().withNameShown(true).withName("Batman").withEmail("batman@gmail.com");
                            if (headerResult.getProfiles() != null) {
                                //we know that there are 2 setting elements. set the new profile above them ;)
                                headerResult.addProfile(newProfile, headerResult.getProfiles().size() - 2);
                            } else {
                                headerResult.addProfiles(newProfile);
                            }
                        }

                        //false if you have not consumed the event and it should close the drawer
                        return true;
                    }
                })
                .withSavedInstance(savedInstanceState)
                .build();*/
        // Create the AccountHeader
       /* AccountHeader headerResult = new AccountHeaderBuilder()
                .withActivity(this)
                .withHeaderBackground(R.mipmap.abc)
                .addProfiles(
                        new ProfileDrawerItem().withName("Mike Penz").withEmail("mikepenz@gmail.com").withIcon(getResources().getDrawable(R.mipmap.abc)),
                        new ProfileDrawerItem().withName("Mike Penz").withEmail("mikepenz@gmail.com").withIcon(getResources().getDrawable(R.mipmap.abc)),
                        new ProfileDrawerItem().withName("Mike Penz").withEmail("mikepenz@gmail.com").withIcon(getResources().getDrawable(R.mipmap.abc)),
                        new ProfileDrawerItem().withName("Mike Penz").withEmail("mikepenz@gmail.com").withIcon(getResources().getDrawable(R.mipmap.abc)),
                        new ProfileDrawerItem().withName("Mike Penz").withEmail("mikepenz@gmail.com").withIcon(getResources().getDrawable(R.mipmap.abc))

                )
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
                        return false;
                    }
                })
                .build();*/

        // Create a few sample profile
        result = new DrawerBuilder()
                .withActivity(this)
                .withTranslucentStatusBar(false)
                .addDrawerItems(
                        new PrimaryDrawerItem().withName("R.string.drawer_item_home"),
                        new PrimaryDrawerItem().withName("R.string.drawer_item_free_play"),
                        new PrimaryDrawerItem().withName("R.string.drawer_item_custom"),
                        new SectionDrawerItem().withName("R.string.drawer_item_section_header"),
                        new SecondaryDrawerItem().withName("R.string.drawer_item_settings"),
                        new SecondaryDrawerItem().withName("R.string.drawer_item_help"),
                        new SecondaryDrawerItem().withName("R.string.drawer_item_open_source"),
                        new SecondaryDrawerItem().withName("R.string.drawer_item_contact")
                )
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        if (drawerItem instanceof Nameable) {
                            Toast.makeText(MainActivity.this, ((Nameable) drawerItem).getName().getText(MainActivity.this), Toast.LENGTH_SHORT).show();
                        }
                        return false;
                    }
                })
                .withSelectedItemByPosition(2)
                .withSavedInstance(savedInstanceState)
                .build();


//Now create your drawer and pass the AccountHeader.Result














        ButterKnife.inject(this);
        //applyKitKatTranslucency();
        initFirstFragment();


    }

    private void initFirstFragment() {
        pariseFragment = new PariseFragment();

        // homeFragment = new HomeFragment();
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.container, pariseFragment);
        llPraise.setBackgroundColor(getResources().getColor(R.color.colorblack));
        //transaction.addToBackStack(null);

        transaction.commit();
    }

    @Override
    protected void initdata() {

    }

    @Override
    protected void initview() {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }


    @OnClick({R.id.ll_home, R.id.ll_praise, R.id.ll_friend, R.id.ll_me})
    public void onClick(View view) {
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        switch (view.getId()) {


            case R.id.ll_friend:
                hideAllFragment();
                if (friendFragment == null ){
                    friendFragment = new FriendFragment();
                    transaction.add(R.id.container, friendFragment,"tagfriend");
                }
                transaction.show(friendFragment);
          






                //transaction.addToBackStack(null);

                transaction.commit();

                llPraise.setBackgroundColor(Color.TRANSPARENT);
                llMe.setBackgroundColor(Color.TRANSPARENT);
                llHome.setBackgroundColor(Color.TRANSPARENT);
                llFriend.setBackgroundColor(getResources().getColor(R.color.colorblack));


                break;
            case R.id.ll_praise:
                hideAllFragment();
                if (pariseFragment ==null){
                    pariseFragment = new PariseFragment();
                    transaction.add(R.id.container, pariseFragment,"tagparise");
                }
                transaction.show(pariseFragment);


                //transaction.addToBackStack(null);

                transaction.commit();
                llPraise.setBackgroundColor(getResources().getColor(R.color.colorblack));
                llMe.setBackgroundColor(Color.TRANSPARENT);
                llHome.setBackgroundColor(Color.TRANSPARENT);
                llFriend.setBackgroundColor(Color.TRANSPARENT);

                break;
            case R.id.ll_me:
                hideAllFragment();
                if (personFragment == null ){
                    personFragment = new MeFragment();
                    transaction.add(R.id.container, personFragment,"tagme");
                }
                transaction.show(personFragment);


                //transaction.addToBackStack(null);

                transaction.commit();
                llMe.setBackgroundColor(getResources().getColor(R.color.colorblack));
                llPraise.setBackgroundColor(Color.TRANSPARENT);
                llHome.setBackgroundColor(Color.TRANSPARENT);
                llFriend.setBackgroundColor(Color.TRANSPARENT);


                break;

            case R.id.ll_home:
                hideAllFragment();
                if (homeFragment == null){
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.container, homeFragment,"taghome");
                }
                transaction.show(homeFragment);


                //transaction.addToBackStack(null);

                transaction.commit();
                llHome.setBackgroundColor(getResources().getColor(R.color.colorblack));
                llMe.setBackgroundColor(Color.TRANSPARENT);
                llPraise.setBackgroundColor(Color.TRANSPARENT);
                llFriend.setBackgroundColor(Color.TRANSPARENT);


                break;


        }
    }

    private void hideAllFragment() {
        if (homeFragment !=null) {
           transaction.hide(homeFragment);

        }
        if (personFragment !=null) {
            transaction.hide(personFragment);

        }
        if (pariseFragment !=null) {
            transaction.hide(pariseFragment);

        }
        if (friendFragment !=null) {
            transaction.hide(friendFragment);

        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        hideAllFragment();
        if (pariseFragment ==null){
            pariseFragment = new PariseFragment();
            transaction.add(R.id.container, pariseFragment,"tagparise");
        }
        transaction.show(pariseFragment);
    }
}
